// first we should init mongoose model so that controller can work.
const mon = require("./app/utils/dbUtil.js");
// connect to mongoDB
mon.connectMongoDB();
// add model into mongoose , can get model by call mongoose.model(modelName);
mon.createModel();

const app = require('./config/express.js');
const config = require('./config/env/config_development.json');
const http = require('http');
const winston = require('winston');

let server = http.createServer(app);


server.listen(config.o2o.port);

///////socket

var io = require("socket.io").listen(server);

io.on('connection',function(socket){

    socket.on('register',function(msg){
    	console.log(msg);
        socket.broadcast.emit("register",{data:"merchant register"});
    });

    socket.on('disconnect', function () {
		console.log('merchant disconnect...')
    });
});
////////socket


// exception handling
process.on('uncaughtException', function (err) {
	winston.error('Caught exception:', err);
});

// Expose app
exports = module.exports = app;




// Logging initialization
winston.info('--');
winston.info(config.o2o.title, 'application started');
winston.info('Environment:\t\t\t', process.env.NODE_ENV);
winston.info('Port:\t\t\t\t', config.o2o.port);
if (process.env.NODE_ENV === 'secure') {
	winston.info('HTTPs:\t\t\t\ton');
}
winston.info('--');

